// Author: Ben
// Project: CR_RandomBackgroundChanger
// Path: C:\code\Abi\CR_RandomBackgroundChanger
// Creation date: 31.07.2008 18:12
// Last modified: 14.11.2008 10:26

#region Using directives
using DevExpress.CodeRush.Core;
using DevExpress.CodeRush.PlugInCore;
using DevExpress.CodeRush.StructuralParser;
using EnvDTE;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Project = DevExpress.CodeRush.Core.Project;
using TextDocument = DevExpress.CodeRush.Core.TextDocument;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Text;
#endregion

namespace CR_RandomBackgroundChanger
{
	/// <summary>
	/// BackgroundChangerPlugIn: Will randomly change the background image for
	/// the Visual Studio text editor every 5 minutes.
	/// </summary>
	public class BackgroundChangerPlugIn : StandardPlugIn
	{
		#region CodeRush-generated code
		/// <summary>
		/// Commenter plug in
		/// </summary>
		public BackgroundChangerPlugIn()
		{
			// Required for Windows.Forms Class Composition Designer support
			InitializeComponent();
			// Load options
			Options.Load();
		} // BackgroundChangerPlugIn()

		/// <summary>
		/// Initialize plug in
		/// </summary>
		public override void InitializePlugIn()
		{
			base.InitializePlugIn();
		} // InitializePlugIn()

		/// <summary>
		/// Finalize plug in
		/// </summary>
		public override void FinalizePlugIn()
		{
			base.FinalizePlugIn();
		} // FinalizePlugIn()
		#endregion

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
			// 
			// BackgroundChangerPlugIn
			// 
			this.OptionsChanged += new DevExpress.CodeRush.Core.OptionsChangedEventHandler(this.BackgroundChangerPlugIn_OptionsChanged);
			this.DocumentActivated += new DevExpress.CodeRush.Core.DocumentEventHandler(this.BackgroundChangerPlugIn_DocumentActivated);
			this.EditorPaint += new DevExpress.CodeRush.Core.EditorPaintEventHandler(this.BackgroundChangerPlugIn_EditorPaint);
			((System.ComponentModel.ISupportInitialize)(this)).EndInit();
		} // InitializeComponent()
		#endregion

		#region Events
		#region BackgroundChangerPlugIn_OptionsChanged
		private void BackgroundChangerPlugIn_OptionsChanged(
			DevExpress.CodeRush.Core.OptionsChangedEventArgs ea)
		{
			Options.Load();

			// Make sure we kill all viewHelpers we got so far
			foreach (ActiveViewBackgroundHelper viewHelper in viewHelpers.Values)
				viewHelper.Dispose();
			viewHelpers.Clear();

			// And try to add the current view again to update it!
			AddCurrentViewToViewHelpers();
		} // BackgroundChangerPlugIn_OptionsChanged()
		#endregion

		#region BackgroundChangerPlugIn_DocumentActivated
		/// <summary>
		/// List of viewHelpers we currently use as a dictionary so we can quickly
		/// check if we already got a viewHelper for a specific view handle!
		/// </summary>
		Dictionary<IntPtr, ActiveViewBackgroundHelper> viewHelpers =
			new Dictionary<IntPtr, ActiveViewBackgroundHelper>();

		private void AddCurrentViewToViewHelpers()
		{
			TextView view = CodeRush.TextViews.Active;
			// If we got no view, we are not able to add anything here!
			if (view == null ||
				(int)view.Handle == 0)
				return;

			// Check if we already have a view helper for this view?
			if (viewHelpers.ContainsKey(view.Handle) == false)
			{
				// No? Then quickly create one and use it from now on!
				viewHelpers.Add(view.Handle,
					new ActiveViewBackgroundHelper(view.Handle));

				// Completely invalidate the view to make sure it gets fully repainted!
				CodeRush.TextViews.Active.Invalidate();
			} // if
		} // AddCurrentViewToViewHelpers()

		/// <summary>
		/// Whenever a document is activated we will try to create a new
		/// ActiveViewBackgroundHelper, which will paint the background image
		/// for us. Same for the EditorPaint event in case we did not have a
		/// valid view handle here yet!
		/// </summary>
		private void BackgroundChangerPlugIn_DocumentActivated(DocumentEventArgs ea)
		{
			//Log.Write("BackgroundChangerPlugIn_DocumentActivated");
			AddCurrentViewToViewHelpers();
		} // BackgroundChangerPlugIn_DocumentActivated(ea)
		#endregion

		#region BackgroundChangerPlugIn_EditorPaint
		/// <summary>
		/// If we are in EditorPaint and view was not valid before in
		/// DocumentActivated, we create the view helper. It is usually to late to
		/// create it for the current draw operation, cause it will cause some
		/// messups like no background image yet or just a background image with no
		/// text on top). But at least the next WM_PAINT message will be handled
		/// correctly!
		/// </summary>
		private void BackgroundChangerPlugIn_EditorPaint(EditorPaintEventArgs ea)
		{
			//Log.Write("BackgroundChangerPlugIn_EditorPaint");
			AddCurrentViewToViewHelpers();
		} // BackgroundChangerPlugIn_EditorPaint(ea)
		#endregion
		#endregion
	} // class BackgroundChangerPlugIn
} // namespace CR_RandomBackgroundChanger
